package org.nyblom.riskcalc;

import org.nyblom.riskcalc.calculate.impl.BaseCalculator;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextSwitcher;
import android.widget.Toast;

public class RiskCalcActivity extends Activity {
    public EditText attacker;
    public EditText defender;
    public TextSwitcher switcher;
    public int[] forces;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
  /*      
        final Button calcButton = (Button) findViewById(R.id.calcButton);
        calcButton.setOnClickListener(new View.OnClickListener(){
        	public void onClick(){
        		
        	}
        });*/
        
        attacker = (EditText) findViewById(R.id.attackerText);
        defender = (EditText) findViewById(R.id.defenderText);
        switcher = (TextSwitcher) findViewById(R.id.resultSwitcher);
        forces = new int[2];
        final Button calcButton = (Button) findViewById(R.id.calcButton);
        calcButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // Perform action on clicks
            	calcHandler(v);
            }
        });
    }
    
	/**
	 * Handles clicking on the calculation button
	 * @param view
	 */
    private void calcHandler(View view){
    	if(attacker.getText().length() == 0 || defender.getText().length() == 0){
    		Toast.makeText(this, "Please enter valid numbers", Toast.LENGTH_LONG).show();
    	} else {
        	BaseCalculator calc = new BaseCalculator();
    		
    		String  a=  (String) attacker.getText().toString();
    		String  d=  (String) defender.getText().toString();
    		forces = new int[]{new Integer(a).intValue(),new Integer(d).intValue()};
        	forces = calc.calculate(forces);
        	Toast.makeText(this, String.valueOf(forces[0])+String.valueOf(forces[1]), Toast.LENGTH_LONG).show();

    	}
    }
    
    private void displayResult(){

    	if(forces[0] != 0){
    		switcher.setText(Integer.toString(forces[0]));
    	} else {
    		switcher.setText(Integer.toString(forces[1]));
    	}
    }
    
    /**
     * Takes the values from the EditTexts and calculates the result
     * @return true if nothing goes wrong
     */
    private boolean calculate(){
    	BaseCalculator calc = new BaseCalculator();
    	
    	String  a=  attacker.getText().toString();
    	String  d=  defender.getText().toString();
    	
    	forces = calc.calculate(new int[]{Integer.getInteger(a),Integer.getInteger(d)});
    	
		return true;
    	
    }
    
	public void openMyDialog(View view) {

    		showDialog(10);

	}
    
	@Override
    public boolean onCreateOptionsMenu(Menu menu){
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.mainmenu, menu);
		return true;
    }
    
 // This method is called once the menu is selected
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
    	// We have only one menu option
    	case R.id.preferences:
    		// Launch Preference activity
    		Intent i = new Intent(RiskCalcActivity.this, MyPreferencesActivity.class);
    		startActivity(i);
    		// Some feedback to the user
    		Toast.makeText(RiskCalcActivity.this, "Enter your user credentials.",
    			Toast.LENGTH_LONG).show();
    		break;


    	}
    	return true;
    }
    
    public void clearTextFields(){
    	attacker.setText("");
    	defender.setText("");
    }

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
        case 10:
            // Create out AlterDialog
            Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Calculations complete!");
            builder.setCancelable(false);
            builder.setPositiveButton("I agree", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    clearTextFields();
                }
            });

            return builder.create();
        }
		//TODO fix ugly hack
		return null;
    }
}